/*--
 * MainWindow.cs: Implementation of the main window for the graphical frontend.
 *--
 * For more information, please visit the Metashell Project site:
 *   http://code.google.com/p/metashell/
 *--
 * (c) 2006 Christopher E. Granade.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; exactly version 2
 * of the License applies to this software.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#region Using Statements

	using GConf;
	using Gecko;	
	using Gtk;

	using Metashell.Core;
	
	using System;
	using System.IO;
	using System.Xml;
		
#endregion

namespace Metashell.Frontends.Graphical {

	public class MainWindow: Gtk.Window {

		#region Private Member Variables
		
			private Metashell.Core.Core core = new Metashell.Core.Core();
			
		#endregion

		#region Graphical Controls
		
			protected Gtk.EventBox InfoBar;
			protected Gecko.WebControl moz;
			protected Gdk.Color infoBarColor = new Gdk.Color(255, 255, 0x65);
			protected Gtk.ScrolledWindow Scroll;
			protected Gtk.EventBox MainSpace;
			protected Gtk.Label InfoBarText;
			protected Gtk.ComboBoxEntry CmdEntryBox;
		
		#endregion
		
		#region Private Methods
		
			private void DisplayNull() {
				moz.LoadUrl("about:blank");
			}
		
			private void DisplayXML(XmlDocument data, string mime) {
				
				if (data == null) {
					DisplayNull();
					return;
				}
				
				StringWriter sw = new StringWriter();
				data.Save(sw);
				
				moz.RenderData(sw.ToString(), "file://" + core.CurrentDirectory, mime);
			}
			
			private void DisplayFile(string path, string mime) {
			
				System.IO.FileStream fs =
					File.Open(path, FileMode.Open, FileAccess.Read);
				XmlDocument xd = new XmlDocument();
				xd.Load(fs);
				
				this.DisplayXML(xd, MIMETypes.XHTML);
				
			}
			
			private void DisplayRes(string respath, string mime) {
				DisplayFile(core.GetResourcePath(respath), mime);
			}
			
			private void DisplayHome() {
				DisplayRes(CommonResources.DefaultOutput, MIMETypes.XHTML);
			}

		#endregion
		
		#region Constructor
		
			public MainWindow (): base ("")	{
				
				Stetic.Gui.Build (this, typeof(MainWindow));
				
				// Allocate the color.
				Gdk.Colormap cmap = Gdk.Colormap.System;
				cmap.AllocColor(ref infoBarColor, false, true);
				
				// Set the color of the infobar.
				InfoBar.ModifyBg(Gtk.StateType.Normal, infoBarColor);
				
				// Register the event handlers for Metashell.
				core.OnError += new Metashell.Core.Core.ErrorEvent(this.FlagError);
				core.OnExit += new Metashell.Core.Core.SimpleEvent(Application.Quit);
				
				// Make a gecko plugin.
				moz = new Gecko.WebControl();
				moz.HeightRequest = 200;
				moz.WidthRequest = 400;
				moz.Show();
				MainSpace.Add(moz);
				
				// Show home.
				try {
					DisplayHome();
			    } catch (Exception e) {
					FlagError("Could not load home page.");
					System.Console.WriteLine(e.Message);
				}
				
			}
		
		#endregion
		
		#region Event Handlers

			protected void OnDeleteEvent(object sender, DeleteEventArgs a) {
				Application.Quit ();
				a.RetVal = true;
			}

			protected virtual void Quit(object sender, System.EventArgs e) {
				Application.Quit();
			}

			protected virtual void OnCloseInfoBarClicked(object sender, System.EventArgs e) {
				
				InfoBar.Visible = false;
				
			}

			protected virtual void OnCmdRunClicked(object sender, System.EventArgs e) {
			
				System.Console.WriteLine("Reached CmdRun event handler.");
				string line = CmdEntryBox.ActiveText;
				System.Console.WriteLine(line);
				CmdEntryBox.AppendText(line);
				CmdEntryBox.Entry.Text = "";
			
				XmlDocument output = core.ProcessLine(line, "xhtml");
				
				DisplayXML(output, MIMETypes.XHTML);
			}
		
		#endregion
		
		#region Public Methods
		
			public void FlagError(string err) {
				
				InfoBarText.Text = err;
				InfoBar.Visible = true;
				
			}
		
		#endregion
		
	}
	
}